/**
 ** This file is part of FK Klistret Extensions. FK Klistret Extensions 
 ** is free software: you can redistribute it and/or modify it under the 
 ** terms of the GNU General Public License as published by the Free 
 ** Software Foundation, either version 3 of the License, or (at your option) 
 ** any later version.

 ** FK Klistret Extensions is distributed in the hope that it will be 
 ** useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** General Public License for more details. You should have received a
 ** copy of the GNU General Public License along with FK Klistret Extensions. 
 ** If not, see <http://www.gnu.org/licenses/>
 */
package test.se.fk.klistret.extension.crud;

import java.util.Arrays;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.Message;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ActiveProfiles;

import com.klistret.cmdb.ci.commons.DestinationReference;
import com.klistret.cmdb.ci.commons.SourceReference;
import com.klistret.cmdb.ci.element.process.change.SoftwareInstallation;
import com.klistret.cmdb.ci.pojo.Element;
import com.klistret.cmdb.ci.pojo.ElementType;

import com.klistret.cmdb.service.ElementService;
import com.klistret.cmdb.service.ElementTypeService;

import com.klistret.cmdb.ci.element.component.Software;

/**
 * Tests installation extension
 * 
 * @author Matthew Young
 * 
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:Spring.cfg.xml" })
@ActiveProfiles(profiles = { "development", "ivy" })
public class InstallationCompleted {

	@Autowired
	protected ElementService elementService;

	@Autowired
	protected ElementTypeService elementTypeService;

	@Autowired
	protected se.fk.klistret.extension.crud.SoftwareInstallationDispatcher client;

	private static final String organization = "se.fk";

	private static final String softwareName = "INF";

	private static final String environmentName = "Test";

	private static final String version = "0068_A02";

	/**
	 * Do not set to planned since the setup method is run for every test case
	 * resulting in many publication installations.
	 */
	private static final String initialState = "Planned";

	private Element element;

	/**
	 * Ends up creating software installations with the exact same values for
	 * each test case
	 */
	
	public void setup() {
		Element environment = elementService
				.unique(Arrays.asList(new String[] { String
						.format("declare namespace pojo=\"http://www.klistret.com/cmdb/ci/pojo\"; /pojo:Element[empty(pojo:toTimeStamp)][pojo:type/pojo:name eq \"{http://www.klistret.com/cmdb/ci/element/context}Environment\"][pojo:name eq \"%s\"]",
								environmentName) }));

		Element software = elementService
				.unique(Arrays.asList(new String[] { String
						.format("declare namespace pojo=\"http://www.klistret.com/cmdb/ci/pojo\"; declare namespace component=\"http://www.klistret.com/cmdb/ci/element/component\"; /pojo:Element[pojo:name eq \"%s\" and empty(pojo:toTimeStamp)][pojo:type/pojo:name eq \"{http://www.klistret.com/cmdb/ci/element/component}Software\"]/pojo:configuration[component:Organization = (\"%s\") and component:Version eq \"%s\"]",
								softwareName, organization, version) }));

		ElementType elementType = elementTypeService
				.get("{http://www.klistret.com/cmdb/ci/element/process/change}SoftwareInstallation");

		/**
		 * Software installation
		 */
		element = new Element();
		element.setType(elementType);
		element.setName("Testing an installation");
		element.setFromTimeStamp(new Date());
		element.setCreateTimeStamp(new Date());

		SoftwareInstallation configuration = new SoftwareInstallation();
		configuration.setName(element.getName());

		DestinationReference destination = new DestinationReference();
		destination.setId(software.getId());
		destination.setName(software.getName());
		destination.setQName(software.getType().getName());
		configuration.setSoftware(destination);

		SourceReference sourceReference = new SourceReference();
		sourceReference.setId(environment.getId());
		sourceReference.setName(environment.getName());
		sourceReference.setQName(environment.getType().getName());
		configuration.setEnvironment(sourceReference);

		configuration.setVersion(((Software) software.getConfiguration())
				.getVersion());
		configuration.setLabel(((Software) software.getConfiguration())
				.getLabel());
		configuration.setState(initialState);

		configuration.getTag().add("alla");

		element.setConfiguration(configuration);
		element = elementService.create(element);
	}

	public void associateApplication() {
		((SoftwareInstallation) element.getConfiguration())
				.setState("Completed");
		elementService.update(element);
	}

	public void ignoreCreatePublications() {
		((SoftwareInstallation) element.getConfiguration()).setState("Planned");
		element.getConfiguration().getTag().add("hello");

		elementService.update(element);
	}

	@Test
	public void dummy() {
		Element element = elementService.get(new Long(729299));

		Message<Element> message = MessageBuilder.withPayload(element)
				.setHeader("function", "CREATE").build();
		client.createPublicationInstallations(message);
	}
}
